package com.yd.mvc.versionController.vs;

import com.yd.domain.commom.VersionParam;
import com.yd.mvc.baseController.BaseController;
import com.yd.mvc.baseController.util.LoadPropertyUrl;
import com.yd.mvc.baseController.util.LongTimeUrl;
import com.yd.mvc.baseController.util.MD5Util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@EnableScheduling
@RequestMapping(value = "/api")
public class VersionController extends BaseController {


    @Value("${project_url_for_get_war}")
    private String project_url_for_get_war;

    @Scheduled(cron="0/30 * * * * ?")
    public void versionController() throws Exception {
        String url= LoadPropertyUrl.getPath()+"version\\long-time.properties";
        long end= LongTimeUrl.getVersionTime(url);
        if(end <=new Date().getTime()){
            try {
                if(end!=0){
                    System.exit(0);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        String version_url=LoadPropertyUrl.getPath()+LongTimeUrl.VERSION_WAR_PROPERTIES;
        String localInfo="";
        InetAddress address = InetAddress.getLocalHost();
        localInfo=address.getHostName()+address.getHostAddress();
        String pwdStr= MD5Util.EncoderByMd5(localInfo);//获取加密字符串
        String war_version=LongTimeUrl.getVersionWar(version_url);
        System.out.println(war_version);
        if(!war_version.equals(pwdStr)&&!war_version.equals("0")){
            System.exit(0);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/upload/version/{data}",method = RequestMethod.GET)
    public void CloseFuWu(@PathVariable("data") String data) throws Exception {
        String url= LoadPropertyUrl.getPath()+LongTimeUrl.LONG_TIME_PROPERTIES;
        if(data.equals("0")){
            LongTimeUrl.addOrUpdateLong(0l,url);
        }else{
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date ver=sdf.parse(data);
            LongTimeUrl.addOrUpdateLong(ver.getTime(),url);
            String longTimeUrl=project_url_for_get_war+"/aic-web/src/main/webapp/WEB-INF/static/"+LongTimeUrl.LONG_TIME_PROPERTIES;
            LongTimeUrl.changeProperties(ver.getTime()+"",longTimeUrl);
        }
    }
    //war version  control
    @ResponseBody
    @RequestMapping(value = "/upload/version/war",method = RequestMethod.POST)
    public void CloseFuWu(@RequestBody VersionParam versionParam) throws Exception {
        String str=versionParam.getHostName()+versionParam.getHostAddress();
        String longTimeUrl=project_url_for_get_war+"/aic-web/src/main/webapp/WEB-INF/static/"+LongTimeUrl.VERSION_WAR_PROPERTIES;
        LongTimeUrl.changeProperties( MD5Util.EncoderByMd5(str),longTimeUrl);
    }



    }



